package com.asset.dispoal.channel.service.common;

import com.asset.dispoal.channel.domain.db.entity.SysChannel;
import com.asset.dispoal.channel.domain.db.mapper.SysChannelMapper;
import com.asset.dispoal.channel.dto.SysChannelRequest;
import com.asset.dispoal.channel.dto.SysChannelResponse;
import com.asset.dispoal.channel.service.converter.SysChannelConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/25$
 */
@Component
public class SysChannelQueryPageCommand implements BaseCommand<SysChannelRequest, Result<PageVO<SysChannelResponse>>> {


    @Autowired
    SysChannelMapper sysChannelMapper;

    @Override
    public Result<PageVO<SysChannelResponse>> execute(SysChannelRequest request) {
        SysChannel sysChannel = SysChannelConverter.INSTANCE.resToDo(request);
        int count = sysChannelMapper.count(sysChannel);
        Page<SysChannel> page = PagingQuery.getPage(count, sysChannel);
        PageVO<SysChannelResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<SysChannel> mapperList = sysChannelMapper.page(page);
            List<SysChannelResponse> sysChannelResponses = SysChannelConverter.INSTANCE.dosToRes(mapperList);
            pageVO.setResult(sysChannelResponses);
        }
        return Result.ok(pageVO);
    }
}
